import { Configuration, App } from '@midwayjs/core'
import * as koa from '@midwayjs/koa'
import * as validate from '@midwayjs/validate'
import * as info from '@midwayjs/info'
import * as orm from '@midwayjs/typeorm'
import { join } from 'path'
import { ReportMiddleware } from './middleware/report.middleware'
import { NotFoundFilter } from './filter/notfound.filter'
import { DefaultErrorFilter } from './filter/default.filter'

@Configuration({
  imports: [
    orm,
    koa,
    validate,
    {
      component: info,
      enabledEnvironment: ['dev', 'test', 'prod'],
    },
  ],
  importConfigs: [join(__dirname, './config')],
})
export class MainConfiguration {
  @App('koa')
  app: koa.Application

  async onReady() {
    console.log('当前环境：' + this.app.getEnv())
    // add middleware
    this.app.useMiddleware([ReportMiddleware])
    // add filter
    this.app.useFilter([NotFoundFilter, DefaultErrorFilter])
  }
}
